package org.jfk.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jfk.core.utils.IdCardUtils;

import com.jfinal.kit.StrKit;
import com.jfinal.validate.Validator;

public abstract  class BaseValidator extends Validator {
	 //校验电话号码正则表达式  
    private static final String phonePattern="\\b(0(\\d{2,3})-\\d{6,9})\\b";  
    //校验手机号码正则表达式  
    private static final String mobilePattern="\\b(1[3,5,7,8,9]\\d{9})\\b";  
    //校验电话手机号码正则表达式  
    private static final String phoneMobilePattern="\\b((1[3,5,7,8,9]\\d{9})|(0(\\d{2,3})-\\d{6,9}))\\b";  
    //校验弱密码正则表达式  （必须6位以上，包含数字、字母、特殊字符，才不是弱密码）
    private static final String weakPasswordPattern="(?=.*[a-zA-Z].*)(?=.*\\d.*)(?=.*[\\W|_].*)(?=.{6,})";  
      
    /**  
     * 校验电话号码  
     * @param field 校验字段  
     * @param errorKey  
     * @param errorMsg  
     */  
    protected void validatePhonePattern(String field,String errorKey,String errorMsg){  
        validateRegex(field, phonePattern, false, errorKey, errorMsg);  
    }  
      
    /**  
     * 验证手机号码  
     * @param field  
     * @param errorKey  
     * @param errorMsg  
     */  
    protected void validateMobilePattern(String field,String errorKey,String errorMsg){  
        validateRegex(field, mobilePattern, false, errorKey, errorMsg);  
    }  
      
    /**  
     * 验证电话手机号码  
     * @param field  
     * @param errorKey  
     * @param errorMsg  
     */  
    protected void validatePhoneMobilePattern(String field,String errorKey,String errorMsg){  
        validateRegex(field, phoneMobilePattern, false, errorKey, errorMsg);  
    }  

    /**  
     * 验证弱密码  
     * @param field  
     * @param errorKey  
     * @param errorMsg  
     */  
    protected void validateWeakPasswordPattern(String field,String errorKey,String errorMsg){  
        boolean isCaseSensitive = false;
        String regExpression = weakPasswordPattern;
        String value = controller.getPara(field);
        if (value == null) {
        	addError(errorKey, errorMsg);
        	return ;
        }
        Pattern pattern = isCaseSensitive ? Pattern.compile(regExpression) : Pattern.compile(regExpression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(value);
        if (!matcher.find())
        	addError(errorKey, errorMsg);
        
    }  

    /**  
     * 验证身份证号码合法性
     * @param field  
     * @param errorKey  
     * @param errorMsg  
     */  
    protected void validateIdCardPattern(String field,String errorKey,String errorMsg){
        boolean isCaseSensitive = false;
        String value = controller.getPara(field);
        //不判断为空的情况
        if (StrKit.isBlank(value)) {
        	return ;
        }
        IdCardUtils iv = new IdCardUtils(); 
        if (!iv.isValidatedAllIdcard(value))
        	addError(errorKey, errorMsg);
    }  
    
    
    public static void main(String[] agrs){
        String value = "p@s5W0rd";
        //String value = "123456";
        boolean isCaseSensitive = false;
        String regExpression1 = "\\b(?=.*\\d)\\b";
        String regExpression2 = "\\b.*\\d\\b";
        String regExpression = "(?=.*[a-zA-Z].*)(?=.*\\d.*)(?=.*[\\W|_].*)(?=.{6,})";
        //String regExpression = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,10}$";
        Pattern pattern = isCaseSensitive ? Pattern.compile(regExpression) : Pattern.compile(regExpression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(value);
        //System.out.println(matcher.matches());
//        if (matcher.find()){
//        	System.out.println("yes");
//        	System.out.println("1"+matcher.group());
//        }else
//        	System.out.println("no");
        
        int i = 0;
        while(matcher.find()){
        	System.out.println("yes "+i+ " : "+matcher.group());
        	i++;
        }
    	System.out.println(i);
    }
    
}
